import 'package:flutter/material.dart';
import 'package:flutter_app/page/home_page.dart';
import 'package:flutter_app/utils/string_utils.dart';
import 'package:flutter_app/view/appbar.dart';
import 'package:flutter_app/view/login_button.dart';
import 'package:flutter_app/view/login_input.dart';
import 'package:flutter_app/view/logineffect.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  bool protect = false;
  bool loginEnable = false;
  String username;
  String passwrod;
  String repassword;
  String code;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(() {
        Navigator.push(
            context, MaterialPageRoute(builder: (context) => HomePage()));
      }, "注册", "登录", centertitle: true),
      body: Container(
        color: Colors.white,
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
              "用户名",
              "请输入用户名",
              onChanged: (text) {
                username = text;
                checkInput();
              },
              focuChanged: (focus) {
                setState(() {
                  protect = !focus;
                });
              },
            ),
            LoginInput(
              "密码",
              "请输入密码",
              lineStretch: true,
              obscureText: true,
              onChanged: (text) {
                passwrod = text;
                checkInput();
              },
              focuChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            LoginInput(
              "确认密码",
              "请再次输入密码",
              lineStretch: true,
              obscureText: true,
              onChanged: (text) {
                repassword = text;
                checkInput();
              },
              focuChanged: (focus) {
                setState(() {
                  protect = focus;
                });
              },
            ),
            LoginInput(
              "邀请码",
              "请输入邀请码",
              lineStretch: true,
              obscureText: true,
              onChanged: (text) {
                code = text;
                checkInput();
              },
            ),
            Padding(
              padding: const EdgeInsets.only(left: 20, right: 20, top: 20),
              child: LoginButton('注册',
                  enable: loginEnable, onPressed: checkParams),
            )
          ],
        ),
      ),
    );
  }
  ///参数是否填入完成
  void checkInput() {
    bool enable;
    if (isNotEmpty(username) &&
        isNotEmpty(passwrod) &&
        isNotEmpty(repassword) &&
        isNotEmpty(code)) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      loginEnable = enable;
    });
  }

  checkParams() {
    String tips;
    if (passwrod != repassword) {
      tips = "两次密码不一致";
    } else if (code.length == 4) {
      tips = "验证码长度不够";
    }
    if (tips != null) {
      print(tips);
      return;
    }
    send();
  }

  void send() {}
}
